<?php

header("Cache-Control: no-cache"); 
header("Pragma: no-cache");

function pwgen4slave($length, $is_upper, $is_lower, $is_number, $symbols, $master_password, $domain)
{
	$lc = "abcdefghijklmnopqrstuvwxyz";
	$nums = "0123456789";
	$uc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	$custom_scheme = "";

	if ($is_upper == "true")
		$custom_scheme .= $uc;

	if ($is_lower == "true")
		$custom_scheme .= $lc;

	if ($is_number == "true")
		$custom_scheme .= $nums;

	$custom_scheme .= $symbols;


	$chartbl01 = base64_encode($domain.$master_password)."dahius internet solutions 2008".$custom_scheme.strlen($length);
	$chartbl02 = base64_encode($domain.$master_password)."netology foundation 2004".$custom_scheme.strlen($length);

	$scheme = md5($chartbl01).md5($chartbl02);
	$scheme = substr($scheme, $length*2).substr($scheme, 32, $length*-4);
	
	$symb_size = strlen($symbols);
	$num_size = strlen($nums);
	$lc_size = strlen($lc);
	$uc_size = strlen($uc);
	
	$symb_count = ($symb_size > 0) ? 1 : 0;

	if ($is_lower == "true" || $is_upper == "true") {
		$rand_ex_size = ($symb_count + ($is_number == "true" ? 1 : 0));
		$letter_count = $length - $rand_ex_size;
	}
	else {
		$letter_count = 0;
	}

	if ($is_number) {
		$letter_count = round($letter_count * 0.7);
		$number_count = $length - ($letter_count + $symb_count);
	}

	$upper_count = 1;
	$pass = "";

	for($k = 0; $k < strlen($scheme); $k++)
	{
		if (is_numeric($scheme[$k])) {
			if ($is_number == "false" || $number_count == 0) continue;
		  	$number_count--;
		}
		else {
			if ($is_lower == "false" &&  $is_upper == "false") continue;

			if ($is_upper == "true" && $upper_count > 0)
			{
				$upchar = $scheme[$k];
				$scheme[$k] = ($scheme[$k] == "z") ? "A" : strtoupper(chr(ord($upchar) + 1));
				$upper_count--;
			}
			
			$letter_count--;	
		}

		$pass .= $scheme[$k];
		$pass = strrev($pass);

		if (($number_count + $letter_count) == 0)
		{
			break;
		}
	}

	if ($is_lower == "false" && $is_upper == "true")
	{
		$pass = strtoupper($pass);
	}

	if ($symb_size > 0)
	{

		for($k = 0; $k < strlen($pass); $k++)
		{
			if (is_numeric($pass[$k]) && $pass[$k] != "0") {
				$value = $k;
				break;
			}
		}

		$symb_pos = ((floor(($value * 4.2) + ($value * 9.6)) + 2) + strlen($custom_scheme)) % $length;
		$cur_sym = $symbols[($value+$length) % $symb_size];
		$front = substr($pass, 0, $symb_pos);
		$back = substr($pass, $symb_pos, $length-$symb_pos-1);
		$pass = $front . $cur_sym . $back;
	}

	return $pass;
}

function pwgen($length, $is_upper, $is_lower, $is_number, $symbols)
{
	$lc = "abcdefghijklmnopqrstuvwxyz";
	$nums = "0123456789";
	$uc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	/*$chars = "";

	if ($is_upper) { $chars .= $uc; }
	if ($is_lower) { $chars .= $lc; }
	if ($is_number) $chars .= $nums;
	$chars .= $symbols;
	 */
	$pass = "";
	$symb_size = strlen($symbols);
	$num_size = strlen($nums);
	$lc_size = strlen($lc);
	$uc_size = strlen($uc);

	// 14'ün katlarına göre listelenecek sembol sayısını hesapla
	$sym_count = floor($length / 14) + 1; 

	while (strlen($pass) < $length - (($symb_size > 0) ? $sym_count : 0))
	{
		$select = rand(0, 3);
		
		switch ($select)
		{
			case 0:
				if ($is_lower == "true")
				{
					$pass .= $lc[rand(0, $lc_size-1)];
				}
				$pass = (((time() %2) == 1) ? strrev($pass) : $pass);

				break;
			case 1:
				if ($is_upper == "true")
				{
					$pass .= $uc[rand(0, $uc_size-1)];
				}
				break;
			case 2:
				if ($is_number == "true")
				{
					$pass .= $nums[rand(0, $num_size-1)];
				}
				$pass = ((((time()*rand()) %2) == 1) ? strrev($pass) : $pass);
				break;
		}
	}

	if ($symb_size > 0)
	{
		while (strlen($pass) < $length)
		{
			$cur_sym = $symbols[rand(0, $symb_size-1)];
			$position = rand(1, $length-2);
			$front = substr($pass, 0, $position);
			$back = substr($pass, $position, $length-$position-1);
			$pass = $front . $cur_sym . $back;
		}
	}

	return $pass;
}

function get_domain ($hostname) {
	dns_get_record($hostname, DNS_A, $authns, $addt);
	return $authns[0]['host'];
}

function get_col_size($l)
{
	switch ($l)
	{
		case 4:
		case 5:
		case 6:
			return 12;
		case 7: 
			return 10;
		case 8:
			return 9;
		case 9:
			return 8;
		case 10:
		case 11:
			return 7;
		case 12:
		case 13:
		case 14:
			return 6;
		case 14:
		case 15:
		case 16:
			return 5;
		case 17:
		case 18:
		case 19:
		case 20:
			return 4;
		case 21:
		case 22:
		case 23:
		case 24:
		case 25:
		case 26:
		case 27:
			return 3;
		default:
			return 2;



	}

}

$type = $_GET["t"];
$length = $_GET["l"];
$is_upper = $_GET["uc"];
$is_lower = $_GET["lc"];
$is_number = $_GET["n"];
$symbols = $_GET["s"];
$output_type = $_GET["o"];
if ($type == "slave")
{
	$master_password = $_GET["mp"];
	$url_info = parse_url($_GET["d"]);
	$domain = ($url_info["scheme"] == "") 
				? parse_url("http://".$url_info["path"], PHP_URL_HOST) 
				: $url_info["host"];

	$result = get_domain($domain);
	if ($result != "") $domain = $result;
}

$col_size = get_col_size($length);

if ($type == "master")
{
	$col = 1;
	$output = "";

	for ($j = 0; $j < ($col_size * 5); $j++)
	{
		$pass = pwgen($length, $is_upper, $is_lower, $is_number, $symbols);
		$output .= $pass;

		if ($output_type == "html") {
			if ($col == $col_size) {
				$output .= "<br/>";
				$col = 0;
			}
			else {
				$output .= " ";	
			}
		}
		else {
			$output .= "\n";	
		}
		$col++;
	}

	echo $output;
}
else if ($type == "slave")
{
	echo pwgen4slave($length, $is_upper, $is_lower, $is_number, $symbols, $master_password, $domain);
}
else
{
	echo "Error";
}

#print_r($_GET);
/*
$chars = getScheme($is_upper, $is_lower, $is_number, $symbols);

for ($k = 0; $k < 64; $k++)
{
#echo	generiatePassword($length, 1 | 2 | 4 | 8)."\n";
#	$pass = createRandomPassword($length, "92sad712saaqwer9123!A+");
#	echo $pass."\n";
	
}
 */
?>
